package demo.vo;

import com.alibaba.fastjson2.JSON;
import org.yaml.snakeyaml.Yaml;

import java.util.Map;

/**
 * 普罗报警规则
 * <blockquote><pre>
 *        alert: InstanceDown
 *        expr: up{job="node"} == 0 and ON(instance) ping_loss_ratio{} == 1
 *        for: 1m
 *        labels:
 *          system: k8s_node
 *          service: "{{ $labels.cluster }}"
 *          target: "{{ $labels.instance }}"
 *        annotations:
 *          summary: "Instance Down"
 *          description: "{{ $labels.instance }} has been down for more than 1 minute"
 *          runbook_url: "http://your-wiki/runbooks/instance-down"
 * </pre><blockquote/>
 */
public class PrometheusAlertRule {

    private String alert;
    private String expr;
    private String duration;
    private Map<String, String> labels;
    private Map<String, String> annotations;


    public static PrometheusAlertRule from(String yamlContent) {
        if (yamlContent == null || yamlContent.isEmpty()) {
            return null;
        }
        PrometheusAlertRule vo = new PrometheusAlertRule();
        // 解析为 Map 结构
        Map<String, Object> data = new Yaml().load(yamlContent);
        vo.setAlert((String) data.get("alert"));
        vo.setExpr((String) data.get("expr"));
        vo.setDuration((String) data.get("for"));
        vo.setLabels((Map<String, String>) data.get("labels"));
        vo.setAnnotations((Map<String, String>) data.get("annotations"));
        return vo;
    }

    public String getAlert() {
        return alert;
    }

    public void setAlert(String alert) {
        this.alert = alert;
    }

    public String getExpr() {
        return expr;
    }

    public void setExpr(String expr) {
        this.expr = expr;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public Map<String, String> getLabels() {
        return labels;
    }

    public void setLabels(Map<String, String> labels) {
        this.labels = labels;
    }

    public Map<String, String> getAnnotations() {
        return annotations;
    }

    public void setAnnotations(Map<String, String> annotations) {
        this.annotations = annotations;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
